<?php


namespace app\admin\controller;

use app\common\model\Admins;
use app\common\model\AuthGroup;
use app\common\model\AuthGroupAccess;
use app\common\model\AuthRule;

use com\Tree;
use think\facade\Cache;
use think\facade\Config;
use think\facade\Db;
use think\facade\Request;
use think\facade\View;
use think\facade\Session;
class AdminGroup extends Base
{
    public function index(){
        $pid = Request::param('pid'); //类别pid
        $where = Request::param('where', []);
        $where = array_filter($where);
        $keyword = Request::param('keyword');

        $list = AuthGroup::where($where)
            ->where(function ($query) use ($keyword) {
                if (!empty($keyword)) {
                    $query->whereLike('title', "%$keyword%");
                }
            })
            ->paginate(10, false, ['query' => request()->param()]);

        $page = $list->render();
        $view = [
            'keyword' => $keyword,
            'page' => $page,
            'list' => $list,
            'pid' => $pid,
        ];
        View::assign($view);
        return View::fetch();
    }
    public function add(){
        if (Request::isPost()){
            $data = Request::post();
            $result = AuthGroup::create($data);
            if ($result) {
                $this->success('用户组添加成功', 'index');
            } else {
                $this->error('用户组添加失败');
            }
        }else{
            $view = [
                'type'=>'添加角色组',
            ];
        }
        View::assign($view);
        return View::fetch();
    }
    public function edit(){
        $id = Request::param('id');
        if (Request::isPost()){
            $data = Request::post();
            $where['id'] = $data['id'];
            AuthGroup::update($data, $where);
            $this->success('修改成功!', 'index');
        }else{
            $info = AuthGroup::find(['id'=>$id]);
            $view = [
                'info'=>$info,
                'type'=>'添加角色组',
            ];
        }
        View::assign($view);
        return View::fetch();
    }
    public function del(){
        $admin_id =  Request::param('admin_id');
        if (Db::name('auth_group')->delete($admin_id)){
            return success('删除角色组成功！','index');
        }else{
            return error('删除用户失败！');
        }
    }
    public function pildel(){
        $id =  Request::param('id');
        $ids = explode(',',$id);
        foreach($ids as $k=>$v){
            $res = Db::name('auth_group')->delete($v);
        }
        if ($res){
            return success('批量删除角色组成功！','index');
        }else{
            return error('批量删除用户失败！');
        }
    }

    public function groupAccess(){
        $admin_rule = Db::name('auth_rule')
            ->field('id, pid, title')
            ->order('sort asc')
            ->select();
        //dump($admin_rule);die;
        $rules = Db::name('auth_group')
            ->where('id', Request::param('id'))
            ->value('rules');
        $list = AdminGroup($admin_rule, $pid = 0, $rules);
        $list = Tree::authChecked($admin_rule, $pid = 0, $rules);
        Cache::set('AuthChecked',$list,3600);
        $list[] = array(
            "id"    => 0,
            "pid"   => 0,
            "title" => "全部",
            "open"  => true
        );
        $group_id = Request::param('id');
        //dump( $group_id);die;
        $idList = AuthRule::column('id');
        $view = [
            'list' => $list,
            'idList' => $idList,
            'group_id' => $group_id,
        ];
        View::assign($view);
        return View::fetch('group_access');
    }

    public function groupSetaccess()
    {
        //$rules = $this->request->post('rules');
        $rules = Request::post('rules');
        if (empty($rules)) {
            return json(['msg'=>'请选择权限!', 'error'=>1]);
        }

        $data = Request::post();
        //dump($data);die;
        $rules = Tree::authNormal($rules);
        $rls = '';
        foreach ($rules as $k=>$v){
            $rls.=$v['id'].',';
        }
        $where['id'] = $data['group_id'];
        $where['rules'] = $rls;

        if (AuthGroup::update($where)) {
            $admin = Session::get('admin');
            $admin['rules'] = $rls;
            Session::set('admin', $admin);

            return success('权限配置成功！');
        } else {
            return error('权限配置失败！');
        }
    }


    public function ajaxTitle ()
    {
        $key = Request::param('name');
        $value = Request::param('param');
        $id = input('id', 0, 'intval');
        $res = AdminGroup::ajaxUsernamexx($value, $id);
        if ($res) {
            $data = array(
                'status' => 'n',
                'info' => '角色组名已经重复，请一个试试'
            );
        } else {
            $data = array(
                'status' => 'y',
                'info' => '验证通过'
            );
        }
        return json($data);
    }

    /**
     * ajax验证用户名
     */
    public function ajaxUsernamexx($title,$id = 0){
        if(empty($title)){
            return 1;
        }
        //组装where条件
        $where = array();
        $where[] = array('title','=',$title);
        if($id > 0){
            $where[] =  array('id','<>',$id);
        }
        return Db::name('auth_group')->where($where)->find();
    }
}